package cn.rc.rcsystem.service.impl;

import cn.rc.rcsystem.domain.User;
import cn.rc.rcsystem.exception.GlobalExceptionCodeEnum;
import cn.rc.rcsystem.mapper.UserMapper;
import cn.rc.rcsystem.result.JSONResult;
import cn.rc.rcsystem.service.IUserService;
import cn.rc.rcsystem.util.AssertUtil;
import cn.rc.rcsystem.util.BaiduAuditUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-12-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper mapper;
    /**
     * 用户注册接口
     * @param user
     * @return
     */
    @Override
    public JSONResult register(User user) {
        // 校验参数是否为空
        AssertUtil.isNotNull(user, GlobalExceptionCodeEnum.COMMON_PARAM_IS_NULL_ERROR);
        // 查询用户是否存在
        Integer phone = user.getPhone();
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("phone", phone);
        User user1 = selectOne(wrapper);
        /* User user1 = mapper.selectByphone(phone);*/
        // 判断用户是否存在
        // 百度ai审核
        AssertUtil.isNull(user1, GlobalExceptionCodeEnum.USER_ALREADY_EXISTS);
        Boolean aBoolean = BaiduAuditUtils.TextCensor(user.getUsername());
        AssertUtil.isTrue(aBoolean, GlobalExceptionCodeEnum.BAIDUAI_ERROR);
        // 添加用户
        insert(user);
        return JSONResult.success();
    }

    /**
     * 根据电话号码查询用户是否存在
     * @param phone
     * @return
     */
    @Override
    public User getPhone(Integer phone) {
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("phone", phone);
        User user = selectOne(wrapper);
        return user;
    }
}
